using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ResourceBlender_Express.Properties;
using System.Data.SQLite;
using System.IO;

namespace ResourceBlender_Express {
    /// <summary>
    /// The first time setup wizard which allows a user to set up a database
    /// </summary>
    public partial class WinInstaller : Window {
        private DatabaseSelector databaseSelector;

        /// <summary>
        /// Initializes a new <see cref="WinInstaller"/>.
        /// </summary>
        public WinInstaller() {
            InitializeComponent();
            Settings.Default.DBPath = System.IO.Path.Combine(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ResourceBlender"), "ResourceBlender.db3");
            databaseSelector = new DatabaseSelector(); // for design time support
            dbTab.Content = databaseSelector;
        }

        /// <summary>
        /// Moves to the previous page.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        protected void btnPrevious_Click(object sender, RoutedEventArgs e) {
            if(tabWizard.SelectedIndex == 1) { // back to the first page, hide the button
                btnPrevious.Visibility = Visibility.Hidden;
            } else {
                btnPrevious.Visibility = Visibility.Visible;
            }
            btnNext.Content = "Next";
            tabWizard.SelectedIndex--;
        }

        /// <summary>
        /// Moves to the next page or finishes the wizard if the last page is active.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        protected void btnNext_Click(object sender, RoutedEventArgs e) {
            int numPages = tabWizard.Items.Count;
            if(databaseSelector.ValidateSettings()) {
                if(tabWizard.SelectedIndex == numPages - 1) { // last page, exit
                    ValidateAndClose();
                } else if(tabWizard.SelectedIndex == numPages - 2) { // moving to last page, change to finish button
                    tabWizard.SelectedIndex++;
                    btnNext.Content = "Finish";
                } else {
                    tabWizard.SelectedIndex++;
                    btnNext.Content = "Next";
                }
                btnPrevious.Visibility = Visibility.Visible;
            }
        }

        /// <summary>
        /// Validates user input then returns a DialogResult of true.
        /// </summary>
        private void ValidateAndClose() {
            if(databaseSelector.ValidateSettings()) {
                Database db = new Database();
                try {
                    if(db.InstanceType == Database.DatabaseType.SQLite) {
                        FileInfo dbFile = new FileInfo(Settings.Default.DBPath);
                        if(!dbFile.Directory.Exists) {
                            dbFile.Directory.Create();
                        }
                        if(dbFile.Exists) {
                            if(MessageBox.Show(this, string.Format("The specified database file already exists. It is strongly recommended that you export the database, then overwrite it and re-import.{0}{0}Re-create the database now?", Environment.NewLine), "Database Exists", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes) {
                                SQLiteConnection.CreateFile(Settings.Default.DBPath);
                            }
                        } else {
                            SQLiteConnection.CreateFile(Settings.Default.DBPath);
                        }
                    }
                    db.CreateTables(true, true, true, true, true, true);
                    Settings.Default.FirstRun = false;
                    Settings.Default.Save();
                    this.DialogResult = true;
                } catch(Exception ex) {
                    MessageBox.Show(this, "Error initializing database: " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                } finally {
                    if(db != null) {
                        try {
                            db.CloseConnection();
                        } catch { }
                    }
                }
            }
        }

        /// <summary>
        /// Opens a URL
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Navigation.RequestNavigateEventArgs"/> instance containing the event data.</param>
        private void lnkWiki_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e) {
            System.Diagnostics.Process.Start(e.Uri.ToString());
        }
    }
}
